 var user=null;

 $(function(){
	
	easyloader.base = '../../js/tools/locale/';
    easyloader.load('easyui-lang-zh_CN.js', function(){  
	            var date=new Date();
	           
             $.getJSON("../../groupbyajax/user!getSessionUser.action?date="+date.getTime(),function(data){
					user=data.user;
					if(user==null)
					{
						alert("请登录！");
					}
				}) ;
				 $.getJSON("../../groupbyajax/user!findCategoryUserList.action?date="+date.getTime(),function(data){
					if(user==null||user.level==4)
					{
						$("#brandBox").hide();
					}else
					{
						data=data.success;
						var option="<option value='-1'>全部</option>";
						for(var i in data)
						{
							option+="<option value='"+data[i].userId+"'>"+((data[i].username=="")?data[i].eusername:data[i].username)+"</option>";
						}
						$("#brand").html(option);
					}
				}) ;
				 $.getJSON("../../groupbyajax/dic!getCategoryList.action",{flag:2},function(data){
				 	if (user == null || user.level == 4) {
						$("#recommendationBox").hide();
					}
					
						var html = "<option value='-1'>全部</option>";
						for (var i = 0; i < data.category.length; i++) {
							html += "<option value='" + data.category[i].id + "'>" + data.category[i].categoryName + "</option>";
						}
						$("#categoryId").html(html);
					
				      });

				var dateTime=getDateTime();  
				  
					 $("#dialog").window({closed: true, modal: false,resizable:false,width:1155,height:528,top:1,left:1,brandId:-1,category:-1});
					
$("#dataGrid").datagrid({
				        height: 450,
						width:1140,
                        pagination: true,
						rownumbers:true,
						striped:true,
						pageList:[15,20,25,30],
						pageSize:15,
						showPageList:true,
						singleSelect:true,
						queryParams:{brandId:-1,category:-1,flag:-1},
                        url: '../../groupbyajax/brand!manageBrandInforAllList.action',
                        columns: [[
						          {field: 'title',title: '标题',width: 150,align: 'left'} ,
						          {field: 'category',title: '所属类别',width: 80,align: 'center'},
						          {field: 'datetime',title: '添加时间',width: 120,align: 'center'}, 
								  {field: 'brand',title: '所属品牌',width: 120,align: 'center'},
						          {field: 'modify',title: '修改',width: 80,align: 'center'}, 
								  {field: 'delete',title: '删除',width:80,align: 'center'},
								  {field: 'recommendation',title: '推荐',width:80,align: 'center'},
								  {field: 'top',title: '置顶',width:80,align: 'center'}
							 ]],
					    onLoadSuccess:function(){
                           if(user.level==1)
							{
								hideColumn("modify");
								$("#imgbox1").hide();
								$("#imgbox2").hide();
								$("#imgbox3").hide();
								$("#imgbox4").hide();
								$("#imgbox5").hide();
								$(".t input").attr("disabled",true);
								$("#content").attr("disabled",true);
								$("#category").attr("disabled",true);
								$("#saveBut").attr("disabled",true);
							}
							if(user.level==5)
							{
								hideColumn("modify");
								$("#imgbox1").hide();
								$("#imgbox2").hide();
								$("#imgbox3").hide();
								$("#imgbox4").hide();
								$("#imgbox5").hide();
								$(".t input").attr("disabled",true);
								$("#content").attr("disabled",true);
								$("#category").attr("disabled",true);
								$("#saveBut").attr("disabled",true);
							}
							if(user.level==6)
							{
								hideColumn("modify");
								
	
								$("#imgbox1").hide();
								$("#imgbox2").hide();
								$("#imgbox3").hide();
								$("#imgbox4").hide();
								$("#imgbox5").hide();
								
								$(".t input").attr("disabled",true);
								$("#content").attr("disabled",true);
								$("#category").attr("disabled",true);
								$("#saveBut").attr("disabled",true);
							}
							if(user.level==7)
							{
								hideColumn("modify");
								
	
								$("#imgbox1").hide();
								$("#imgbox2").hide();
								$("#imgbox3").hide();
								$("#imgbox4").hide();
								$("#imgbox5").hide();
								
								$(".t input").attr("disabled",true);
								$("#content").attr("disabled",true);
								$("#category").attr("disabled",true);
								$("#saveBut").attr("disabled",true);
							}
							if(user.level==8)
							{
								hideColumn("modify");
								
	
								$("#imgbox1").hide();
								$("#imgbox2").hide();
								$("#imgbox3").hide();
								$("#imgbox4").hide();
								$("#imgbox5").hide();
								
								$(".t input").attr("disabled",true);
								$("#content").attr("disabled",true);
								$("#category").attr("disabled",true);
								$("#saveBut").attr("disabled",true);
							}
							
							if(user.level==4)
						    {
								hideColumn("recommendation");
								hideColumn("top");
								
							}

							 
						}
			        });	

					 
					  
					
			});	
			
});	


function del(id)
{
$.messager.confirm('删除', '你确定要删除记录吗?', function(r){
		                          if(r)
								  {
var date =new Date();
	$.getJSON("../../groupbyajax/brand!delBrand.action?times="+date.getTime(),{brandId:id},function(data){
		if(data.success)
		{
			$("#dataGrid").datagrid('reload');
		}
	});
	}
	});

}

function top(id,flag)
{
	var date=new Date();
	$.getJSON("../../groupbyajax/brand!topBrand.action?date="+date.getTime(),{brandId:id,flag:flag},function(data){
		if(data.success)
		{
			
             if(flag==1)
			{
				
				$("#tu"+id).parent().html("<a href='javascript:void(0)' onclick='top("+id+",0,this);' id='tu"+id+"'><font color='blue'>置顶</color></font></a>");
			}else
			{
				
				$("#tu"+id).parent().html("<a href='javascript:void(0)' onclick='top("+id+",1,this);' id='tu"+id+"'><font color='red'>未置顶</color></font>");
			}
		}
	});
}
/**
 * 显示
 */
function showColumn(field)
{
	$(".datagrid-view2 .datagrid-header div table tr th").each(function(){
		if($(this).attr("field")==field)
		{
			$(this).show();
			return false;
		}
	});
$(".datagrid-column-"+field).show();
}
/**
 * 隐藏
 * @param {Object} field
 */
function hideColumn(field)
{	
$(".datagrid-view2 .datagrid-header div table tr th").each(function(){
		if($(this).attr("field")==field)
		{
			$(this).hide();
			return false;
		}
	});
$(".datagrid-column-"+field).hide();
}

function search()
{
	var title=$("#title").val();
	var startdate=$("#startdate").val();
	var enddate=$("#enddate").val();
	var brandId=$("#brandId").val();
	if(brandId==null)
	{
		brandId=-1;
	}
	var category=$("#categoryId").val();
	var recommendation=$("#recommendation").val();
	if(recommendation==null)
	{
		recommendation=-1;
	}
	$('#dataGrid').datagrid({
				  striped: true,
				  queryParams:{title:title,startTime:startdate,endTime:enddate,brandId:brandId,category:category,flag:recommendation}
			   });
}

function recommendation(id,flag)
{
	var date=new Date();
	$.getJSON("../../groupbyajax/brand!recommendation.action?date="+date.getTime(),{brandId:id,flag:flag},function(data){
		if(data.success)
		{
             if(flag==1)
			{
				$("#u"+id).parent().html("<a href='javascript:void(0)' onclick='recommendation("+id+",0,this);' id='u"+id+"'><font color='blue'>推荐</color></font></a>");
			}else
			{
				$("#u"+id).parent().html("<a href='javascript:void(0)' onclick='recommendation("+id+",1,this);' id='u"+id+"'><font color='red'>未推荐</color></font>");
			}
		}
	});
}
